<?php

Yii::import('ext.gapi');

class StateController extends Controller
{
	//public $layout = nil;
	
	public function actionIndex($id)
	{
		$store = Store::model()->findByPk($id);
		
		$fees = Fee::model()->findAllByAttributes(array('store_id'=>$store->id, 'visible'=>true), array('order'=>'date_time DESC', 'limit'=>20));
		$account = Account::model()->findAllByAttributes(array('store_id'=>$store->id), array('order'=>'date DESC', 'limit'=>20));
		
		$this->render('index',array('store'=>$store, 'fees'=>$fees, 'account'=>$account));
	}
	
	public function actionStats($id)
	{	
		$this->layout = nil;
		$account = Account::model()->findAllByAttributes(array('store_id'=>$id), array('order'=>'date DESC', 'limit'=>20));
		$this->render('stats', array('account'=>$account));
	}
	public function actionFee($store_id)
	{
		$this->layout = nil;
    $model=new Fee;
    $model->name = 'name';
    $model->visible = true;
    $model->store_id = $store_id;

    // uncomment the following code to enable ajax-based validation
    /*
    if(isset($_POST['ajax']) && $_POST['ajax']==='fee-fee-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
		*/
    if(isset($_POST['Fee']))
    {
      $model->attributes=$_POST['Fee'];
      if($model->save())
      {
        $this->redirect(array('state/index','id'=>$store_id));
      }
    }
    $this->render('fee',array('model'=>$model));
	}
	public function actionPrice($id)
	{
		$this->layout = nil;
    $store=Store::model()->findByPk($id);

    // uncomment the following code to enable ajax-based validation
    /*
    if(isset($_POST['ajax']) && $_POST['ajax']==='fee-fee-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
		*/
    if(isset($_POST['Store']))
    {
      $store->attributes=$_POST['Store'];
      if($store->save())
      {
        $this->redirect(array('state/index','id'=>$id));
      }
    }
    $this->renderPartial('_price',array('store'=>$store));
	}
	public function actionDelete($id)
	{	
		Yii::app()->db->createCommand("
				UPDATE fee SET visible=false WHERE id=".$id."
			")->execute();
		Yii::app()->clientScript->registerMetaTag('no-cache', nil, 'Pragma');
		echo 'El pago se ha eliminado.';
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}